home *** CD-ROM | disk | FTP | other *** search
- /* btcreat */
- #include <stdio.h>
- #include <btextern.h>
-
- int btcreat (filnam)
- char filnam[];
-
- /* create new instance of an index */
- {
- int i, occurs, ptrarea, keyarea, *ip, left;
- char *cp;
-
- if ((fp = fopen(filnam, "wrb")) == NULL)
- BTSETCOD (NULL, NULL, 4); /* file open error */
- if ((openkt = btopnkt ()) == -1)
- BTSETCOD (openkt, NULL, 63); /* no control block */
- if (!(btfilar[openkt].infobuf =
- (int *)calloc (LBLEN, sizeof (char))))
- BTSETCOD (openkt, NULL, 7); /* no memory */
- if (!(btfilar[openkt].filbuf =
- (int *)calloc (LBLEN, sizeof (char))))
- BTSETCOD (openkt, NULL, 7); /* no memory */
- strcpy (btfilar[openkt].curkey," ");
- btfilar[openkt].keylen = gkeylen;
- strcpy (btfilar[openkt].filnam, gfilnam);
- btfilar[openkt].handle = openkt;
- btfilar[openkt].root = 1;
- btfilar[openkt].fdesc = fp;
-
- /* initialize info index record */
- ip = btfilar[openkt].infobuf;
- *ip++ = 1; /*value of root record */
- *ip++ = gkeylen; /*set key length */
- *ip = 1; /*rec # 1 unavailable */
-
- /* now initialize rest with zeros */
- left = 1 + (LBLEN / 2 - 3); /* left to be initialized */
- while (--left)
- *++ip = 0; /* rest are available */
- movmem ((char *)btfilar[openkt].infobuf,
- (char *)btfilar[openkt].filbuf, LBLEN);
- /* write index info record from buffer */
- if (( i = btwrit (openkt,0)))
- BTSETCOD (openkt,0,i); /* set write error */
-
- /* now initialize root record */
- occurs = (LBLEN - 1) / (gkeylen + 3);
- btfilar[openkt].ikeyptr =
- (char *)(btfilar[openkt].filbuf + occurs);
-
- /* initialize pointer area with NULL */
- ip = btfilar[openkt].filbuf;
- ip3 = ip + occurs;
- while (ip < ip3)
- *ip++ = NULL;
-
- /* intialize keyarea with null strings */
- cp = btfilar[openkt].ikeyptr; /* initialize pointer */
- cp3 = (char *) btfilar[openkt].filbuf + LBLEN;
- while (cp < cp3)
- *cp++ = '\0';
-
- /* initialize flag to pointer to data */
- cp = (char *) btfilar[openkt].filbuf;
- cp += (LBLEN - 1);
- *cp = 'Y';
-
- /* write root record */
- if (( i = btwrit (openkt, 1)))
- BTSETCOD (openkt, 1, i); /* set write error */
-
- /* return value to outside world */
- gfilhand = openkt;
- gretcode = 0;
- return (0);
- }
- /* end of btcreat */